package cn.yunhe.service;

import cn.yunhe.dao.IStockDao;
import cn.yunhe.entity.Stock;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;;

import java.util.List;

/**
 * 邓永凯
 */
public class IStockService {
    SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
    IStockDao iStockDao = sqlSession.getMapper(IStockDao.class);

    /**
     * 调用dao方法
     *
     * @return List<Stock>集合
     */

    public List<Stock> getAllStock() {
        List<Stock> stockList = iStockDao.getAllStock();
        sqlSession.close();
        return stockList;
    }

    /**
     * 根据仓库id 查询出仓库的信息
     *
     * @param stockId 仓库id
     * @return stock
     * @Param(value = "stockId")
     */

    public Stock getStockById(int stockId) {
        Stock stock = null;
        stock = iStockDao.getStockById(stockId);
        sqlSession.close();
        return stock;
    }

    /**
     * 仓库信息修改
     *
     * @param stock
     * @return
     */

    public boolean stockUpdate(Stock stock) {
        boolean flag = false;
        flag = iStockDao.stockUpdate(stock);
        if (flag) {
            sqlSession.commit();
            sqlSession.close();
        }
        return flag;
    }

    /**
     * 仓库信息删除
     *
     * @param stockId 仓库编号
     * @return
     */

    public boolean stockDelete(int stockId) {
        boolean flag = false;
        flag = iStockDao.stockDelete(stockId);
        if (flag) {
            sqlSession.commit();
            sqlSession.close();
        }
        return flag;
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    public boolean stockDel(List list) {
        boolean flag=false;
        flag=iStockDao.stockDel(list);
        if (flag){
            sqlSession.commit();
            sqlSession.close();
        }
        return flag;
    }

    /**
     * 模糊查询
     * @param keyword
     * @return
     */
    public List<Stock> stockQuery(String keyword){
        List<Stock> stockList = null;
        stockList=iStockDao.stockQuery(keyword);
        sqlSession.close();
        return stockList;
    }


}
